home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 19 / CU Amiga Magazine's Super CD-ROM 19 (1998)(EMAP Images)(GB)[!][issue 1998-02].iso / CUCD / Online / NNTPd / server / xthread.c < prev   
Encoding:
C/C++ Source or Header  |  2002-11-01  |  3.4 KB  |  152 lines

  1. /* This file (and only this file - not the entire nntp distribution) is
  2.  * hereby placed in the public domain.  Use it as you see fit, but if you
  3.  * manage to find some wonderful use for this code elsewhere, it would be
  4.  * nice to receive mention for it.
  5.  *
  6.  * - Tim Iverson
  7.  *   iverson@xstor.com -/- uunet!xstor!iverson
  8.  *   3/28/91
  9.  *   modified by Wayne Davison (davison@borland.com) to work with trn 2.0
  10.  *   10/6/91
  11.  */
  12.  
  13. #include "common.h"
  14. #ifndef lint
  15. static char sccsid[] = "$Id: xthread.c,v 1.2 1994/11/01 06:08:21 sob Exp sob $";
  16. #endif
  17. #ifdef XTHREAD
  18.  
  19. # ifdef __GNUC__
  20. #  define alloca __builtin_alloca
  21. # endif
  22.  
  23. char *thread_name();
  24.  
  25. /* Usage: XTHREAD [DBINIT|THREAD]
  26.  *
  27.  * DBINIT    dump the contents of the db.init file to stdout
  28.  * THREAD    dump the contents of the thread file for the current
  29.  *        newsgroup to stdout (default if no argument).
  30.  *
  31.  * N.B. These two files are not ascii and no attempt is made at converting
  32.  *    native byte size to any type of standard whatsoever.  This'll have
  33.  *    to be fixed if this command is to be integrated into the protocol.
  34.  *
  35.  * This command is not documented in rfc977.
  36.  */
  37.  
  38. void
  39. xthread(argc, argv)
  40. int    argc;
  41. char    *argv[];
  42. {
  43.     register FILE    *fp;
  44.     struct stat    s;
  45.     char        *buf, *file, *what;
  46.  
  47.     /* can't transfer threads, only read 'em */
  48.     if (!canread)
  49.     {
  50.         printf("%d You only have permission to transfer, sorry.\r\n",
  51.             ERR_ACCESS);
  52.         (void) fflush(stdout);
  53.         return;
  54.     }
  55.  
  56.     /* "parse" the argument */
  57.     if (argc == 2 && !strcasecmp(argv[1], "dbinit"))
  58.     {
  59.         file = thread_name("*******");
  60.         what = "db.init";
  61.         strcpy(index(file, '*'), what);
  62.     }
  63.     else if (argc == 1 || argc == 2 && !strcasecmp(argv[1], "thread"))
  64.     {
  65.         if (!threadfile)
  66.         {
  67.             printf("%d You are not currently in a newsgroup.\r\n",
  68.                 ERR_NCING);
  69.             (void) fflush(stdout);
  70.             return;
  71.         }
  72.         file = threadfile;
  73.         what = "thread";
  74.     }
  75.     else
  76.     {
  77.         printf("%d Usage: XTHREAD [DBINIT|THREAD]\r\n", ERR_CMDSYN);
  78.         (void) fflush(stdout);
  79.         return;
  80.     }
  81.  
  82.     /* try to open the file to be transfered */
  83.     if (!(fp = fopen(file, "r")))
  84.     {
  85.         printf("%d %s file is not available.\r\n", ERR_FAULT, what);
  86.         (void) fflush(stdout);
  87. #ifdef SYSLOG
  88.         if (!strcmp(what, "db.init"))
  89.             syslog(LOG_ERR, "xthread: fopen %s: %m", file);
  90. #endif
  91.         return;
  92.     }
  93.  
  94.     /* tell 'em how much binary data is coming down the pike */
  95.     fstat(fileno(fp), &s);
  96.     printf("%d %u bytes of %s file follows verbatim (binary!)\r\n",
  97.         OK_BIN, s.st_size, what);
  98.  
  99.     /* copy the file verbatim to stdout */
  100. #ifdef __GNUC__
  101.     if (buf = alloca(s.st_size))
  102.     {
  103.         /* ah-so! got lotsa memoree */
  104.         read(fileno(fp), buf, s.st_size);
  105.         fwrite(buf, s.st_size, 1, stdout);
  106.     }
  107.     else
  108. #endif
  109.     {
  110.         int        bytes;
  111.         char        buf[BUFSIZ];
  112.  
  113.         while (bytes = fread(buf, 1, sizeof buf, fp))
  114.             fwrite(buf, bytes, 1, stdout);
  115.     }
  116.  
  117.     fputs("\r\n.\r\n", stdout);
  118.     fflush(stdout);
  119.     fclose(fp);
  120. }
  121.  
  122. /* Change a newsgroup name into the name of the thread data file.  We
  123. ** subsitute any '.'s in the group name into '/'s (unless LONG_THREAD_NAMES
  124. ** is defined), prepend the path, and append the '/.thread' (or '.th') on to
  125. ** the end.
  126. */
  127. char *
  128. thread_name(group)
  129. char *group;
  130. {
  131.     static char name_buff[MAXPATHLEN];
  132. #ifdef LONG_THREAD_NAMES
  133.     char group_buff[512];
  134.     register char *ptr;
  135.  
  136.     strcpy(group_buff, group);
  137.     ptr = group = group_buff;
  138.     while ((ptr = index(ptr, '/'))) {
  139.         *ptr = '.';
  140.     }
  141. #endif
  142. #ifdef SUFFIX
  143.     sprintf(name_buff, "%s/%s%s", threaddir, group, SUFFIX);
  144. #else
  145.     sprintf(name_buff, "%s/%s", threaddir, group);
  146. #endif
  147.  
  148.     return name_buff;
  149. }
  150.  
  151. #endif /* not XTHREAD */
  152.